//
//  LPTForgotPassViewController.m
//  LPTClient
//
//  Created by apple on 2018/12/4.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "LPTForgotPassViewController.h"
#import "NSString+Category.h"
#import "MMProgressHUD.h"
#import "LPTBaseRequest.h"
@interface LPTForgotPassViewController ()
{
    BOOL isDelay;
}


@property (nonatomic, weak) IBOutlet UILabel *phoneLab;
@property (nonatomic, weak) IBOutlet UILabel *passLab;
@property (nonatomic, weak) IBOutlet UILabel *codeLab;

@property (nonatomic, weak) IBOutlet UITextField *phoneTextField;
@property (nonatomic, weak) IBOutlet UITextField *passTextField;
@property (nonatomic, weak) IBOutlet UITextField *codeTextField;
@property (nonatomic, weak) IBOutlet UIButton *codeButton;
@property (nonatomic, weak) NSTimer *m_timer;
@property (nonatomic, assign) NSInteger count;

@property (nonatomic, assign) BOOL m_isRP;

@end

@implementation LPTForgotPassViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.m_isRP = NO;
    }
    return self;
}

+ (instancetype)instance
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    LPTForgotPassViewController *loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LPTForgotPassViewControllerID"];
    return loginVC;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
    if (self.m_isRP) {
        
        self.title = @"修改密码";
        self.phoneLab.text = @"原密码";
        self.codeLab.text = @"新密码";
        self.passLab.text = @"确认新密码";
        
        self.phoneTextField.placeholder = @"请输入原密码";
        self.codeTextField.placeholder = @"请输入新密码";
        self.passTextField.placeholder = @"请再次输入新密码";
        self.codeButton.hidden = YES;
        
    }
}

- (void)isResetPasswordVC:(BOOL)isRP
{
    self.m_isRP = isRP;
}

// 验证码点击
- (IBAction)codeClickAction:(id)sender
{
    [self getVerifyCode];
}

- (void)commitReChangePass:(NSString *)originPass newPass:(NSString *)newPass
{
    [MMProgressHUD showWithStatus:@"提交中"];

    NSDictionary *body = @{@"password": originPass,
                           @"new_password" : newPass
                           };
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/reset_password" withBody:body withMethod:YTKRequestMethodPOST];
    
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismissWithSuccess:@"修改密码成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismissWithError:request.msg];
        
    }];
}

// 提交
- (IBAction)commitRegisterAction:(id)sender
{
    [MMProgressHUD showWithStatus:@"提交中"];
    
    NSString *telephone = self.phoneTextField.text;
    NSString *password = self.passTextField.text;
    NSString *code = self.codeTextField.text;
    
    if (self.m_isRP) {
        if (telephone.length == 0)
        {
            [MMProgressHUD dismissWithError:@"请输入原密码"];
            return;
        }
        if (password.length == 0)
        {
            [MMProgressHUD dismissWithError:@"请输入新密码"];
            return;
        }
        if (code.length == 0)
        {
            [MMProgressHUD dismissWithError:@"请确认新密码"];
            return;
        }
        if (![password isEqualToString:code]) {
            [MMProgressHUD dismissWithError:@"密码输入密码不一致"];
            return;
        }
        [self commitReChangePass:telephone newPass:password];
        return;
    }
    
    if (!telephone.isMobileNumber)
    {
        [MMProgressHUD dismissWithError:@"手机号格式有误"];
        return;
    }
    if (password.length == 0)
    {
        [MMProgressHUD dismissWithError:@"请输入密码"];
        return;
    }
    if (code.length == 0)
    {
        [MMProgressHUD dismissWithError:@"请输入验证码"];
        return;
    }
    
    NSDictionary *body = @{@"mobile": telephone,
                           @"password" : password,
                           @"code": code
                           };
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/forgot_password" withBody:body withMethod:YTKRequestMethodPOST];
    
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismissWithSuccess:@"修改密码成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismissWithError:request.msg];
        
    }];
    
}

- (void)getVerifyCode
{
    [MMProgressHUD showWithStatus:@"获取验证码中"];
    
    NSString *telephone = self.phoneTextField.text;
    
    if (!telephone.isMobileNumber)
    {
        [MMProgressHUD dismissWithError:@"请输入正确的手机号"];
        return;
    }
    
    NSDictionary *body = @{};
    NSString *apiName = [NSString stringWithFormat:@"/common/send_sms_code/%@", telephone];
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:apiName withBody:body withMethod:YTKRequestMethodGET];
    
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        // 8336  是成功
        [MMProgressHUD dismissWithSuccess:@"验证码发送成功"];
        [self codeStart];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismissWithError:request.msg];
        
    }];
}

- (void)codeStart
{
    if (isDelay) {
        return;
    }
    _count = 60;
    [self.codeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.codeButton setTitle:@"60s" forState:UIControlStateNormal];
    self.m_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange:) userInfo:nil repeats:YES];
    
    isDelay = YES;
}

- (void)timeChange:(NSTimer *)timer
{
    if (_count > 1)
    {
        _count--;
        NSString *str = [NSString stringWithFormat:@"%lds",(long)_count];
        [self.codeButton setTitle:str forState:UIControlStateNormal];
    }
    else
    {
        isDelay = NO;
        [timer invalidate];
        self.m_timer = nil;
        [self.codeButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
